<template>
  <div class="app-container">
    <!-- 搜索框 -->

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="负责专家" prop="zj">

        <el-col :span="12">
          <el-autocomplete class="inline-input" v-model="state1" :fetch-suggestions="querySearch" placeholder="请输入负责专家"
            @select="handleSelect" style="width:200px">
          </el-autocomplete>
        </el-col>

      </el-form-item>

      <el-form-item label="活动名称" prop="hdName">
        <el-input v-model="queryParams.hdName" placeholder="请输入活动名称" clearable @keyup.enter.native="handleQuery"
          style="width:200px" />
      </el-form-item>

      <!-- <el-form-item label="活动时间" prop="time">
        <el-date-picker clearable v-model="queryParams.time" type="date" value-format="yyyy-MM-dd" placeholder="请选择活动时间"
          style="width:200px">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="活动地点" prop="didian">
        <el-input v-model="queryParams.didian" placeholder="请输入活动地点" clearable @keyup.enter.native="handleQuery"
          style="width:200px" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 新增、导出按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:huodongbiao:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:huodongbiao:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 显示活动列表 -->
    <el-table v-loading="loading" :data="huodongbiaoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="活动名称" align="center" prop="hdName" />
      <el-table-column label="专家" align="center" prop="zj" />
      <el-table-column label="产业学院" align="center" prop="cyxy" />
      <el-table-column label="活动地点" align="center" prop="didian" />
      <!-- 
      <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[6].visible" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column> -->


      <el-table-column label="活动时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}年{m}月{d}日{h}时') }}</span>

        </template>
      </el-table-column>


      <!-- <el-table-column label="参加班级" align="center" prop="bj" /> -->
      <!-- <el-table-column label="活动内容" align="center" prop="hdnr" /> -->
      <el-table-column label="总体评分" align="center" prop="hdpf" width="180">
        <template slot-scope="scope">
          <!-- <el-rate prop="scope.row.hdpj" disabled show-score text-color="#ff9900" score-template="hdpf">
          </el-rate> -->
          <el-rate v-model="scope.row.hdpf" disabled show-score text-color="#ff9900" score-template="">
          </el-rate>

        </template>
      </el-table-column>

      <!-- <el-table-column label="总体评分" align="center" prop="hdpf" /> -->
      <!-- <el-table-column label="整体评价" align="center" prop="hdpj" /> -->
      <!-- <el-table-column label="评价附件" align="center" class-name="small-padding fixed-width" prop="fj">
        <template slot-scope="scope">

        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button @click="xqhandleClick(scope.row)" type="text" size="small">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-download" @click="downloadAnnex(scope.row)">附件</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:huodongbiao:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:huodongbiao:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" :inline="true">
        <el-form-item label="活动名称" prop="hdName">
          <el-input v-model="form.hdName" placeholder="请输入活动名称" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="专家" prop="zj">
          <el-col :span="12">
            <el-autocomplete class="inline-input" v-model="state1" :fetch-suggestions="querySearch" placeholder="请输入负责专家"
              @select="handleSelect" style="width:400px">
            </el-autocomplete>
          </el-col>
        </el-form-item>

        <el-form-item label="活动时间" prop="time">
          <div class="block" style="width: 400px;">
            <!-- <el-date-picker v-model="form.time" type="datetime" placeholder="选择日期时间" align="right"
              :picker-options="pickerOptions" style="width: 400px;">
            </el-date-picker> -->

            <el-date-picker v-model="form.time" type="datetime" placeholder="选择日期时间" style="width: 400px;"
              value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </div>
        </el-form-item>

        <!-- <el-form-item label="活动时间" prop="time">
          <el-date-picker clearable v-model="form.time" type="date" value-format="yyyy-MM-dd" placeholder="请选择活动时间"
            style="width: 400px;">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="活动地点" prop="didian">
          <el-input v-model="form.didian" placeholder="请输入活动地点" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="负责的产业学院" prop="cyxy">
          <el-input v-model="form.cyxy" placeholder="请输入负责的产业学院" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="参加的班级" prop="bj">
          <el-input v-model="form.bj" placeholder="请输入参加的班级" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="活动整体评价" prop="hdpj">
          <el-input v-model="form.hdpj" placeholder="请输入活动整体评价" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="总体评分" prop="hdbf">
          <div class="block">
            <el-rate v-model="form.hdpf" :colors="colors">
            </el-rate>
          </div>
        </el-form-item>
        <br>
        <el-form-item label="活动内容">
          <editor v-model="form.hdnr" :min-height="192" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="评价附件" prop="fj">
          <file-upload @change="handleFileChange" v-model="form.fj" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 详情页 -->
    <el-dialog :title="title" :visible.sync="xqDialog" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" :inline="true">
        <el-form-item label="活动名称" prop="hdName">
          <el-input v-model="form.hdName" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="专家" prop="zj">
          <el-input v-model="form.zj" style="width: 400px;" />
        </el-form-item>

        <el-form-item label="活动时间" prop="time">
          <div class="block" style="width: 400px;">
            <el-date-picker v-model="form.time" type="datetime" style="width: 400px;" value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="活动地点" prop="didian">
          <el-input v-model="form.didian" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="负责的产业学院" prop="cyxy">
          <el-input v-model="form.cyxy" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="参加的班级" prop="bj">
          <el-input v-model="form.bj" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="活动整体评价" prop="hdpj">
          <el-input v-model="form.hdpj" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="总体评分" prop="hdbf">
          <div class="block">
            <el-rate v-model="form.hdpf" :colors="colors">
            </el-rate>
          </div>
        </el-form-item>
        <br>
        <el-form-item label="活动内容">
          <editor v-model="form.hdnr" :min-height="192" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="评价附件" prop="fj">
          <file-upload @change="handleFileChange" v-model="form.fj" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="xqDialog = false">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listHuodongbiao, getHuodongbiao, delHuodongbiao, addHuodongbiao, updateHuodongbiao } from "@/api/system/huodongbiao";
import { listZhuanjiabiao, getZhuanjiabiao, delZhuanjiabiao, addZhuanjiabiao, updateZhuanjiabiao } from "@/api/system/zhuanjiabiao";
import { getToken } from "@/utils/auth";

export default {
  name: "Huodongbiao",
  data() {
    return {
      xqDialog: false,
      colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
      zjarr: [],
      restaurants: [],
      state1: '',
      state2: '',
      filePath: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // huodong表格数据
      huodongbiaoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hdName: null,
        zj: null,
        time: null,
        didian: null,
        cyxy: null,
        bj: null,
        hdnr: null,
        hdpf: null,
        hdpj: null,
        fj: null,
        sj: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hdName: [
          { required: true, message: "活动名称不能为空", trigger: "blur" }
        ],
        zj: [
          { required: true, message: "专家不能为空", trigger: "blur" }
        ],

        time: [
          { required: true, message: "活动时间不能为空", trigger: "blur" }
        ],
        didian: [
          { required: true, message: "活动地点不能为空", trigger: "blur" }
        ],
        cyxy: [
          { required: true, message: "负责的产业学院不能为空", trigger: "blur" }
        ],
        hdpf: [
          { required: true, message: "活动总体评分不能为空", trigger: "change" }
        ],



      }
    };
  },
  created() {
    this.getList();
    this.getZjList();
  },
  mounted() {
  },
  methods: {

    // 详情页
    xqhandleClick(row) {
      console.log(row);
      this.reset();
      const hdId = row.hdId;
      getHuodongbiao(hdId).then(response => {
        this.form = response.data;

        this.xqDialog = true;
        this.title = "活动详情";
      });
    },



    //查询专家表
    getZjList() {
      this.loading = true;
      listZhuanjiabiao(this.queryParams).then(response => {
        this.zhuanjiabiaoList = response.rows;
        this.total = response.total;
        this.loading = false;

        var OldArrObject = response.rows;
        var NewArrObject = OldArrObject.map((item, index) => {
          return Object.assign({}, { 'value': item.zjName })
        })
        this.zjarr = NewArrObject;
        console.log(this.zjarr);
        this.restaurants = this.loadAll();
      });
    },

    loadAll() {
      return this.zjarr;
    },
    handleSelect(item) {
      console.log(item);
      this.form.zj = item.value;
    },

    querySearch(queryString, cb) {
      var restaurants = this.restaurants;
      var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },
    // 上传下载
    downloadAnnex(row) {


      const zj = row.zj;
      listHuodongbiao(zj).then(response => {
        this.form = response.rows[0];
        console.log(response.rows[0]);
        this.$download.zip(this.form.fj, `${new Date().getTime()}.zip`);

      });

    },


    handleFileChange(file) {
      upload(this.fileInfo.file).then((response) => {
        console.log("文件上传成功", response);
      }).catch((error) => {
        console.log("文件上传失败", error);
      });
      this.fj = file.path;
    },


    /** 查询huodong列表 */
    getList() {
      this.loading = true;
      listHuodongbiao(this.queryParams).then(response => {
        this.huodongbiaoList = response.rows;
        this.total = response.total;
        this.loading = false;

      });

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hdId: null,
        hdName: null,
        zj: null,
        time: null,
        didian: null,
        cyxy: null,
        bj: null,
        hdnr: null,
        hdpf: null,
        hdpj: null,
        fj: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.hdId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const hdId = row.hdId || this.ids
      getHuodongbiao(hdId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.hdId != null) {
            updateHuodongbiao(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHuodongbiao(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hdIds = row.hdId || this.ids;
      this.$modal.confirm('是否确认删除活动编号为"' + hdIds + '"的数据项？').then(function () {
        return delHuodongbiao(hdIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/huodongbiao/export', {
        ...this.queryParams
      }, `huodongbiao_${new Date().getTime()}.xlsx`)
    },



    pickerOptions() {
      shortcuts: [{
        text: '今天',
        onClick(picker) {
          picker.$emit('pick', new Date());
        }
      }, {
        text: '昨天',
        onClick(picker) {
          const date = new Date();
          date.setTime(date.getTime() - 3600 * 1000 * 24);
          picker.$emit('pick', date);
        }
      }, {
        text: '一周前',
        onClick(picker) {
          const date = new Date();
          date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
          picker.$emit('pick', date);
        }
      }]
    },


  }
};
</script>
